#   ============================================================================
#   Author: Youhua Xu
#	Date:	2021-12-01
#	
#   ============================================================================

#   Make a build dir for compilation
MDIR := $(shell pwd)
WRKDIR = $(MDIR)/build
BINDIR = $(MDIR)/bin

#	make build & binary dirs
.base:
	if ! [ -e $(WRKDIR) ]; then mkdir $(WRKDIR) ; fi;
	touch $(WRKDIR)/.base;
	if ! [ -e $(BINDIR) ]; then mkdir $(BINDIR) ; fi;
	touch $(BINDIR)/.base;
	# if ! [ -e $(DEBUG) ]; then mkdir $(DEBUG) ; fi;
	# touch $(DEBUG)/.base;
#   ============================================================================
#   Set the source file path
vpath %.c   main:src:tests
vpath %.h   include
vpath %.o 	build
vpath .base build

INCLUDES = -I $(MDIR)/include

GCC			= gcc
CC			= arm-linux-gnueabihf-gcc
# CC			= gcc
CCFLAG  	= -Wall #-fcommon
OPTFLAG		= #-O2 #-ffast-math #( not recognized by intel compiler )
LIBS		= -lm -lpthread
LDFLAG      = 

# CCFLAG		+= -D_SPI_DEBUG_	# turn on SPI information
CCFLAG		+= -D_DEBUG_DACX0508_
# CCFLAG		+= -D_DEBUG_PS_SPI_
# CCFLAG      += -D_DEBUG_ADG714_
CCFLAG		+= -D_DEBUG_CCD_DRV_
# CCFLGA		+= -D_DEBUG_CCD_DRV_PARAMS_	# print drv parameters
# CCFLAG		+= -D_DEBUG_ads5294_

#CCFLAG		+= -D_REPORT_SHUTTER_STATUS_	#这个选项仅在DEBUG时使用，正式使用时需要关掉

CCFLAG      += -D_ENABLE_PINNING_BEFORE_EXPOSURE_	# 控制曝光开始前是否pinning
CCFLAG      += -D_ENABLE_REFRESH_BEFORE_EXPOSURE_   # 
CCFLAG		+= -D_ENABLE_VOD_ADJUST_		        # 控制在读出前\读出后是否调节偏置电压VOD


#################################################
# 2024-10-20:
CCFLAG		+= -D_ENABLE_FAST_READ_
#################################################

# CCFLAG	+= -D_DEBUG_FLOAT2BIN_	#debug浮点数转二进制

#   http://www.tuicool.com/articles/NBfeYj
ifeq ($(shell uname -s),Linux)
	LDFLAG	+= -Wl,--no-as-needed
	
	ifeq ($(shell which arm-linux-gnueabihf-gcc),)
		CC	= gcc
	else
		CC	= arm-linux-gnueabihf-gcc
#		CC	= gcc
	endif
endif

ifeq ($(shell uname -s),Darwin)
	LDFLAG	+= -framework Accelerate #(-framework Accelerate is for Mac OSX)
	CC		= gcc
endif

%.o: %.c .base
	cd $(WRKDIR);$(CC) $(OMPFLAG) $(OPTFLAG) $(CCFLAG) $(INCLUDES) -c ../$< -o $*.o

# MAIN
MAIN_ADG714     		= ADG714_MAIN.o

MAIN_DACX0508   		= DACx0508_MAIN.o

MAIN_READOUT    		= CCD290_readout.o

MAIN_AD9508				= AD9508_MAIN.o
MAIN_AD9508_RESET       = AD9508_reset_MAIN.o
MAIN_AD9508_SYNC        = AD9508_sync_MAIN.o
MAIN_AD9508_READ        = AD9508_read_MAIN.o

MAIN_ADS5294 			= ADS5294_MAIN.o
MAIN_ADS5294_RESET 		= ADS5294_reset_MAIN.o
MAIN_ADS5294_SYNC		= ADS5294_sync_MAIN.o
MAIN_ADS5294_REG_UPDATE = ADS5294_reg_update_MAIN.o

MAIN_DAISY              = DaisyChain.o

MAIN_AD_CLK     		= ADC2REG_CLK_CFG.o

MAIN_156M				= CLK_156M.o
MAIN_AFE 				= AFE_PW_CTL.o


MAIN_CCD_DRV_DEBUG		= Debug_CCD_Drv.o

MAIN_VTRANS_CONFIG		= MAIN_VTRANS_Config.o

MAIN_DDR_WR_CFG			= DDR_WR_CFG.o

MAIN_IDELAY_CFG			= IDELAY_CFG.o
MAIN_IDELAYCRT_RST  	= idelayctrl_rst.o

MAIN_BONN_SHUTTER		= MAIN_BONN_SHUTTER.o

MAIN_CCD290_EXPOSURE	= CCD290_exposure.o

MAIN_CCD290_PIN			= CCD290_pin.o
MAIN_CCD290_REFRESH		= CCD290_refresh.o

MAIN_RD_DRIVE_CONFIG	= CCD290_drive_config.o

MAIN_DCDS_WEIGHTS_CONFIG= DCDS_WEIGHTS_Config.o

MAIN_OPA862_EN			= OPA862_EN.o
MAIN_CCD_TYPE			= CCD_type.o

MAIN_SCHMIDT_SOCKET_SERVER		= schmidt_socket_server_multi_threads.o
# MAIN_SCHMIDT_SOCKET_SERVER_DBG 	= schmidt_socket_server_multi_threads_dbg.o

MAIN_CSST_SOCKET_SERVER		= socket_csst_ccd_test.o
MAIN_CSST_SCAN_SOCKET_SERVER= socket_csst_ccd_scan_test.o

MAIN_CAM_POWER          = cam_power.o

MAIN_EXTRACT_PCB_INFO	= Extract_PCB_Info.o

MAIN_RESETMEM			= ResetMem.o

MAIN_FASTER_READOUT     = CCD290_readout_faster.o

MAIN_CAPTURE_WAVE		= capture_wave.o

# DEPS
DEPS	= iniparser.o dictionary.o misc.o common.o spi.o ADG714.o DACx0508_config.o \
		spi_ps.o spi_ps_new.o AD9508.o Daisy.o BONN_SHUTTER.o float2bin.o \
		spi_float_ps.o CCD290_DRV_Config.o ADS5294.o ADC2REG_CLK.o ReadOut.o  \
		SchmidtCAM_Config.o socket_pinning.o socket_power.o

all: adg714_cfg dacx0508_cfg ccd290_readout adc2reg_clk ddr_wr_en vtrans_cfg \
    cfg_idelay rst_idelayctrl shutter ccd290_exposure \
    ccd290_drive_config dcds_w_cfg ad9508_cfg daisy_cfg en_156M_clk \
    ad9508_reset ad9508_sync ad9508_read_reg AFE_PW ads5294_cfg ads5294_reset \
	ads5294_sync ads5294_reg_update schmidt_socket_server \
	ccd290_pin freadout ccd290_refresh debug_ccd_drv csst_ccd_test_socket csst_scan_ccd_test_socket \
	capture_wave

#     sread ccd290_pin ccd290_refresh run_socket_server_44s run_socket_server_scan_44s \
# 	run_socket_server_ad23high run_socket_server ccd290_readout_ad23high run_socket_server_scan

cam_power: ${MAIN_CAM_POWER} ${DEPS}
	${CC} ${OPTFLAG} ${LDFLAG} $(addprefix build/,$(notdir $^)) ${LIBS} -o $(BINDIR)/$@

adg714_cfg: ${MAIN_ADG714} ${DEPS}
	${CC} ${OPTFLAG} ${LDFLAG} $(addprefix build/,$(notdir $^)) ${LIBS} -o $(BINDIR)/$@

en_156M_clk: ${MAIN_156M} ${DEPS}
	${CC} ${OPTFLAG} ${LDFLAG} $(addprefix build/,$(notdir $^)) ${LIBS} -o $(BINDIR)/$@

AFE_PW: ${MAIN_AFE} ${DEPS}
	${CC} ${OPTFLAG} ${LDFLAG} $(addprefix build/,$(notdir $^)) ${LIBS} -o $(BINDIR)/$@

ad9508_cfg: ${MAIN_AD9508} ${DEPS}
	${CC} ${OPTFLAG} ${LDFLAG} $(addprefix build/,$(notdir $^)) ${LIBS} -o $(BINDIR)/$@

ad9508_reset: ${MAIN_AD9508_RESET} ${DEPS}
	${CC} ${OPTFLAG} ${LDFLAG} $(addprefix build/,$(notdir $^)) ${LIBS} -o $(BINDIR)/$@
	
ad9508_sync: ${MAIN_AD9508_SYNC} ${DEPS}
	${CC} ${OPTFLAG} ${LDFLAG} $(addprefix build/,$(notdir $^)) ${LIBS} -o $(BINDIR)/$@

ad9508_read_reg: ${MAIN_AD9508_READ} ${DEPS}
	${CC} ${OPTFLAG} ${LDFLAG} $(addprefix build/,$(notdir $^)) ${LIBS} -o $(BINDIR)/$@

ads5294_cfg: ${MAIN_ADS5294} ${DEPS}
	${CC} ${OPTFLAG} ${LDFLAG} $(addprefix build/,$(notdir $^)) ${LIBS} -o $(BINDIR)/$@

ads5294_reset: ${MAIN_ADS5294_RESET} ${DEPS}
	${CC} ${OPTFLAG} ${LDFLAG} $(addprefix build/,$(notdir $^)) ${LIBS} -o $(BINDIR)/$@
	
ads5294_sync: ${MAIN_ADS5294_SYNC} ${DEPS}
	${CC} ${OPTFLAG} ${LDFLAG} $(addprefix build/,$(notdir $^)) ${LIBS} -o $(BINDIR)/$@

ads5294_reg_update: ${MAIN_ADS5294_REG_UPDATE} ${DEPS}
	${CC} ${OPTFLAG} ${LDFLAG} $(addprefix build/,$(notdir $^)) ${LIBS} -o $(BINDIR)/$@

dacx0508_cfg: ${MAIN_DACX0508} ${DEPS}
	${CC} ${OPTFLAG} ${LDFLAG} $(addprefix build/,$(notdir $^)) ${LIBS} -o $(BINDIR)/$@
	
daisy_cfg: ${MAIN_DAISY} ${DEPS}
	${CC} ${OPTFLAG} ${LDFLAG} $(addprefix build/,$(notdir $^)) ${LIBS} -o $(BINDIR)/$@

ccd290_readout: ${MAIN_READOUT} ${DEPS}
	${CC} ${OPTFLAG} ${LDFLAG} $(addprefix build/,$(notdir $^)) ${LIBS} -o $(BINDIR)/$@

freadout: ${MAIN_FASTER_READOUT} ${DEPS}
	${CC} ${OPTFLAG} ${LDFLAG} $(addprefix build/,$(notdir $^)) ${LIBS} -o $(BINDIR)/$@

ccd290_readout_ad23high: ${MAIN_READOUT_AD23HIGH} ${DEPS}
	${CC} ${OPTFLAG} ${LDFLAG} $(addprefix build/,$(notdir $^)) ${LIBS} -o $(BINDIR)/$@

cfg_lt: ${MAIN_LT_CFG} ${DEPS}
	${CC} ${OPTFLAG} ${LDFLAG} $(addprefix build/,$(notdir $^)) ${LIBS} -o $(BINDIR)/$@
	
cfg_rt: ${MAIN_RT_CFG} ${DEPS}
	${CC} ${OPTFLAG} ${LDFLAG} $(addprefix build/,$(notdir $^)) ${LIBS} -o $(BINDIR)/$@

adc2reg_clk: ${MAIN_AD_CLK} ${DEPS}
	${CC} ${OPTFLAG} ${LDFLAG} $(addprefix build/,$(notdir $^)) ${LIBS} -o $(BINDIR)/$@

ddr_wr_en: ${MAIN_DDR_WR_CFG} ${DEPS}
	${CC} ${OPTFLAG} ${LDFLAG} $(addprefix build/,$(notdir $^)) ${LIBS} -o $(BINDIR)/$@

save_data: ${MAIN_SAVE_DDR} ${DEPS}
	${CC} ${OPTFLAG} ${LDFLAG} $(addprefix build/,$(notdir $^)) ${LIBS} -o $(BINDIR)/$@

rst_idelayctrl: ${MAIN_IDELAYCRT_RST} ${DEPS}
	${CC} ${OPTFLAG} ${LDFLAG} $(addprefix build/,$(notdir $^)) ${LIBS} -o $(BINDIR)/$@

cfg_idelay: ${MAIN_IDELAY_CFG} ${DEPS}
	${CC} ${OPTFLAG} ${LDFLAG} $(addprefix build/,$(notdir $^)) ${LIBS} -o $(BINDIR)/$@

shutter: ${MAIN_BONN_SHUTTER} ${DEPS}
	${CC} ${OPTFLAG} ${LDFLAG} $(addprefix build/,$(notdir $^)) ${LIBS} -o $(BINDIR)/$@
	
ccd290_exposure: ${MAIN_CCD290_EXPOSURE} ${DEPS}
	${CC} ${OPTFLAG} ${LDFLAG} $(addprefix build/,$(notdir $^)) ${LIBS} -o $(BINDIR)/$@

ccd290_exposure_sl: ${MAIN_CCD290_EXPOSURE} ${DEPS}
	${CC} ${OPTFLAG} ${LDFLAG} $(addprefix build/,$(notdir $^)) ${LIBS} -o $(BINDIR)/$@

ccd290_exposure_thorlabs: ${MAIN_CCD290_EXPOSURE_THORLABS} ${DEPS}
	${CC} ${OPTFLAG} ${LDFLAG} $(addprefix build/,$(notdir $^)) ${LIBS} -o $(BINDIR)/$@

ccd290_pin: ${MAIN_CCD290_PIN} ${DEPS}
	${CC} ${OPTFLAG} ${LDFLAG} $(addprefix build/,$(notdir $^)) ${LIBS} -o $(BINDIR)/$@


ccd290_refresh: ${MAIN_CCD290_REFRESH} ${DEPS}
	${CC} ${OPTFLAG} ${LDFLAG} $(addprefix build/,$(notdir $^)) ${LIBS} -o $(BINDIR)/$@

ccd290_drive_config: ${MAIN_RD_DRIVE_CONFIG} ${DEPS}
	${CC} ${OPTFLAG} ${LDFLAG} $(addprefix build/,$(notdir $^)) ${LIBS} -o $(BINDIR)/$@

dcds_w_cfg: ${MAIN_DCDS_WEIGHTS_CONFIG} ${DEPS}
	${CC} ${OPTFLAG} ${LDFLAG} $(addprefix build/,$(notdir $^)) ${LIBS} -o $(BINDIR)/$@	

vtrans_cfg: ${MAIN_VTRANS_CONFIG} ${DEPS}
	${CC} ${OPTFLAG} ${LDFLAG} $(addprefix build/,$(notdir $^)) ${LIBS} -o $(BINDIR)/$@

schmidt_socket_server: ${MAIN_SCHMIDT_SOCKET_SERVER} ${DEPS}
	${CC} ${OPTFLAG} ${LDFLAG} $(addprefix build/,$(notdir $^)) ${LIBS} -o $(BINDIR)/$@

csst_ccd_test_socket: ${MAIN_CSST_SOCKET_SERVER} ${DEPS}
	${CC} ${OPTFLAG} ${LDFLAG} $(addprefix build/,$(notdir $^)) ${LIBS} -o $(BINDIR)/$@

csst_scan_ccd_test_socket: ${MAIN_CSST_SCAN_SOCKET_SERVER} ${DEPS}
	${CC} ${OPTFLAG} ${LDFLAG} $(addprefix build/,$(notdir $^)) ${LIBS} -o $(BINDIR)/$@

debug_ccd_drv: ${MAIN_CCD_DRV_DEBUG} ${DEPS}
	${CC} ${OPTFLAG} ${LDFLAG} $(addprefix build/,$(notdir $^)) ${LIBS} -o $(BINDIR)/$@
	
capture_wave: ${MAIN_CAPTURE_WAVE} ${DEPS}
	${CC} ${OPTFLAG} ${LDFLAG} $(addprefix build/,$(notdir $^)) ${LIBS} -o $(BINDIR)/$@
####################################

f2b: main/MAIN_Float2Bin.c src/float2bin.c
	gcc -I ./include main/MAIN_Float2Bin.c src/float2bin.c -o bin/f2b

dcds:
	cd dcds_extractor; make; cd --;

.PHONY:clean tidy run
clean: .base
	rm -rf $(WRKDIR);
tidy:
	make clean; rm -rf $(BINDIR) $(WRKDIR)
